﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;

namespace PreProcessor
{
    public static class TTSetCreater
    {
        public static void TTSCreate(string currentDBPath, string trainingDBPath, string testingDBPath, 
                int trainingDBCount, bool takeAllTestingLeft, int testingDBCount = 0)
        {
            Random rnd = new Random();

            if (Directory.Exists(trainingDBPath))
                Directory.Delete(trainingDBPath, true);

            if (Directory.Exists(testingDBPath))
                Directory.Delete(testingDBPath, true);       

            Directory.CreateDirectory(trainingDBPath);
            Directory.CreateDirectory(testingDBPath);

            string[] directories = Directory.GetDirectories(currentDBPath);

            for (int i = 0; i < directories.Length; i++)
            {
                string directoryTraining = trainingDBPath + "\\" + new DirectoryInfo(directories[i]).Name;
                string directoryTesting = testingDBPath + "\\" + new DirectoryInfo(directories[i]).Name;

                Directory.CreateDirectory(directoryTraining);
                Directory.CreateDirectory(directoryTesting);

                List<string> tempFiles = Directory.GetFiles(directories[i]).ToList();

                for (int j = 0; j < trainingDBCount; j++)
                {
                    int random = rnd.Next(0, tempFiles.Count);
                    string pathToWrite = directoryTraining + "\\" + Path.GetFileName(tempFiles[random]);
                    File.Copy(tempFiles[random], pathToWrite);
                    tempFiles.RemoveAt(random);
                }

                if (!takeAllTestingLeft)
                {
                    for (int j = 0; j < testingDBCount; j++) //take random (int)testDBCount files from tempDB to testDB
                    {
                        int random = rnd.Next(0, tempFiles.Count);
                        string pathToWrite = directoryTesting + "\\" + Path.GetFileName(tempFiles[random]);
                        File.Copy(tempFiles[random], pathToWrite);
                        tempFiles.RemoveAt(random);
                    }
                }
                else // take all testing left
                {
                    while (tempFiles.Count > 0)
                    {
                        int random = rnd.Next(0, tempFiles.Count);
                        string pathToWrite = directoryTesting + "\\" + Path.GetFileName(tempFiles[random]);
                        File.Copy(tempFiles[random], pathToWrite);
                        tempFiles.RemoveAt(random);
                    }
                }                
            }
        }
    }
}
